﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="~/Areas/Back/lib/layui/layui.js"></script>
    <link href="~/Areas/Back/lib/css/layui.css" rel="stylesheet" />
    <title>编辑</title>
</head>
<body>
    <div class="layui-container" style="margin-top:20px; margin-bottom:150px">
        @*onsubmit="return false" 禁止form表单提交，我们要使用ajax*@
        <form class="layui-form" id="addForm" onsubmit="return false">
            @*标题*@
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" readonly value="@ViewBag.A.UserName" id="UserName" class="layui-input" />
                </div>
            </div>
            @*封面*@
            <div class="layui-form-item">
                <label class="layui-form-label">上传图片</label>
                <div class="layui-input-block">
                    <input type="file" id="uploadImg" name="uploadImg" />
                    <input type="hidden" value="@ViewBag.A.ImgUrl" id="ImgUrl" />
                </div>
            </div>
            @*填充图片位置*@
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <img id="Photo" src="/@ViewBag.A.ImgUrl" class="layui-upload-img" style="width:180px; height:140px" />

                </div>
            </div>
            @*个人简介*@
            <div class="layui-form-item">
                <label class="layui-form-label">个人简介</label>
                <div class="layui-input-block">
                    <textarea id="Cont" lay-filter="content" style="display: none;">
                        @ViewBag.A.Cont
                    </textarea>
                </div>
            </div>

            @*按钮*@
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="subAdd">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>
</body>
<script>
    layui.use(['form','layedit','upload'], function () {
        var form = layui.form;
        var layedit = layui.layedit;
        var upload = layui.upload;
        var layer = layui.layer;
        var $ = layui.$;
        var index = layedit.build('Cont'); //富文本编辑器
        form.verify({
            content: function () {
                layedit.sync(index);
            }
        });
        //图片上传
        upload.render({
            elem: "#uploadImg",
            url: "UploadPhoto",//图片上传地址
            done: function (res) {
                //上传成功
                if (res.success == true) {
                    $("#ImgUrl").val(res.path)
                    $("#Photo").attr("src", "/" + res.path);
                }

            }
        });
        //开始处理点击提交
        form.on('submit(subAdd)', function () {
            //ajax请求
            $.ajax({
                url: "EditDel",
                method: "post",
                dataType: "json",
                data: {
                    UserName: $("#UserName").val(),
                    ImgUrl: $("#ImgUrl").val(),
                    Cont:layedit.getContent(index)
                },
                success: function (res) {
                    if (res.success == true) {
                        layer.msg(res.msg);
                        //关闭当前窗口，并且刷新列表
                        //1.获取弹窗的编号
                        var index = parent.layer.getFrameIndex(window.name);
                        //2.关闭窗口
                        parent.layer.close(index);
                        //3.刷新父级页面
                        parent.location.reload();
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</html>